import myAxios from "@/request";
import { ApiResponse, UserInfo } from "@/types";
import { blogRequest } from "@/api/blog";

interface userRegisterRequest {
  phone: string;
  password: string;
}

interface userLoginRequest {
  phone: string;
  password: string;
}

interface userSimpleInfo {
  id: number;
  username: string;
  avatar: string;
  signature: string;
}

/**
 * 用户注册
 * @param params
 * @returns
 */
export const userRegister = async (params: userRegisterRequest) => {
  return await myAxios.request({
    url: "/user/register",
    method: "post",
    data: params,
  });
};

/**
 * 用户登录
 * @param params
 * @returns
 */
export const userLogin = async (params: userLoginRequest) => {
  return await myAxios.post<
    ApiResponse<{ tokenValue: string; userInfo: UserInfo }>
  >(`/user/login`, {
    phone: params.phone,
    password: params.password,
  });
};

/**
 * 获取当前用户信息
 * @returns
 */
export const getCurrentUser = async () => {
  return await myAxios.get<ApiResponse<UserInfo>>("/user/getCurrentUserInfo");
};

/**
 * 获取指定用户信息
 * @param userId
 * @returns
 */
export const getUserInfo = async (userId: number) => {
  return await myAxios.get<ApiResponse<UserInfo>>(
    `/user/getUserInfo/${userId}`
  );
};

/**
 * 判断是否登录
 * @returns
 */
export const isLogin = async () => {
  return await myAxios.request({
    url: "/user/isLogin",
    method: "get",
  });
};

/**
 * 用户登出
 * @returns
 */
export const logout = async () => {
  return await myAxios.request({
    url: "/user/logout",
    method: "get",
  });
};

/**
 * 获取用户信息
 * @param blogId
 * @returns
 */
export const getByBlogId = async (blogId: number) => {
  return await myAxios.request<ApiResponse<UserInfo>>({
    url: "/user/" + blogId,
    method: "get",
    data: blogId,
  });
};

/**
 * 关注/取关用户
 * @param userId
 * @returns
 */
export const doFollow = async (userId: number) => {
  return await myAxios.request({
    url: `/user/follow/${userId}`,
    method: "post",
  });
};

/**
 * 获取关注列表
 * @returns
 */
export const getFollowList = async () => {
  return await myAxios.request({
    url: "/user/followList",
    method: "get",
  });
};

/**
 * 获取粉丝列表
 * @returns
 */
export const getFanList = async () => {
  return await myAxios.request({
    url: "/user/fanList",
    method: "get",
  });
};

/**
 * 获取搜索用户列表
 * @param key
 * @returns
 */
export const getSearchUserList = async (key: string) => {
  const response = await myAxios.get<ApiResponse<userSimpleInfo[]>>(
    `/user/search?key=${key}`
  );
  return response.data;
};
